#include "cli_api.h"
#include <time.h>


static void timedeal_command_usage(const char *prog)
{
    printf("  Usage: %s [OPTIONS]\r\n", prog);
    printf("  Options:\r\n");
    printf("  set clock value(timestamp)                Set init clock time\r\n");
    printf("  trans time value                          Transform timestamp to clock time\r\n");
    printf("  trans clock valude(y m d h m s)           Transform clock time to timestamp\r\n");
    printf("  get clock                                 Get the clock time\r\n");
    printf("\r\n");
}



static int set_clocktime(int argc, char **argv)
{
    time_t time = 0;
    uint32_t ret = 0;

    if (!strcmp(argv[2], "clock")) {
        time = strtoul(argv[3], NULL, 10);
        printf("timestamp : %d.\n", time);
        if (-1 == stime(&time)) {
            printf("set clock time error.\n");
            ret = -1;
        }
    } else {
        printf("param invalid!\n");
        ret = -1;
    }

    return ret;
}

static int get_clocktime(int argc, char **argv)
{
    time_t t = 0;
	struct tm *gmt;
    uint32_t ret = 0;

    if (!strcmp(argv[2], "clock")) {
        t = time(NULL);
        gmt = gmtime(&t);

        printf("%d年%02d月%02d日",(1900+gmt->tm_year), (1+gmt->tm_mon), gmt->tm_mday);
        printf(" %02d:%02d:%02d\n", (gmt->tm_hour), gmt->tm_min,gmt->tm_sec);
    } else {
        printf("param invalid!\n");
        ret = -1;
    }

    return ret;
}

static int trans_timestamp(int argc, char **argv)
{
    uint32_t ret = 0;
    time_t time = 0;
    struct tm *gmt;


    time = strtoul(argv[3], NULL, 10);
    printf("timestamp : %d.\n", time);

	gmt = gmtime(&time);

	printf("%d年%02d月%02d日", (1900+gmt->tm_year), (1+gmt->tm_mon), gmt->tm_mday);
	printf(" %02d:%02d:%02d\n", (gmt->tm_hour), gmt->tm_min, gmt->tm_sec);

    return ret;
}

static int trans_clocktime(int argc, char **argv)
{
    uint32_t ret = 0;
    time_t time = 0;
    struct tm gmt;


    gmt.tm_year = strtoul(argv[3], NULL, 10) - 1900;
    gmt.tm_mon  = strtoul(argv[4], NULL, 10) - 1;
    gmt.tm_mday = strtoul(argv[5], NULL, 10);
    gmt.tm_hour = strtoul(argv[6], NULL, 10);
    gmt.tm_min  = strtoul(argv[7], NULL, 10);
    gmt.tm_sec  = strtoul(argv[8], NULL, 10);
    
	printf("%d年%02d月%02d日", (1900+gmt.tm_year), (1+gmt.tm_mon), gmt.tm_mday);
	printf(" %02d:%02d:%02d\n", gmt.tm_hour, gmt.tm_min, gmt.tm_sec);

    time = mktime(&gmt);
    printf("timestamp : %d.\n", time);

    return ret;
}


static int trans_time_format(int argc, char **argv)
{
    uint32_t ret = 0;
 
    if (!strcmp(argv[2], "time")) {
        trans_timestamp(argc, argv);
        printf("trans timestamp to clock time.\n");
    }    
    else if (!strcmp(argv[2], "clock")) {
        trans_clocktime(argc, argv);
        printf("trans clock time to timestamp.\n");
    } else {
        printf("param invalid!\n");
        return -1;
    }

    return ret;
}

void timedeal_command(char *outbuf, int len, int argc, char **argv)
{
    if (argc == 1) {
        timedeal_command_usage(argv[0]);
        return;
    }
    if (!strcmp(argv[1], "set")) {
        set_clocktime(argc, argv);
    } else if (!strcmp(argv[1], "get")) {
        get_clocktime(argc, argv);
    } else if (!strcmp(argv[1], "trans")) {
        trans_time_format(argc, argv);
    } else {
       printf("param invalid!\n");
    }

    return;
}


static const struct cli_command_st timedeal_cli_cmd[] = 
{
    [0] =
    {
        .name            = "timedeal",
        .help            = "timedeal [timestamp and clock time deal]",
        .function        = timedeal_command
    },
};

int time_cmds_cli_register(void) 
{
    int ret = 0;

    ret = cli_register_commands(timedeal_cli_cmd, sizeof(timedeal_cli_cmd)/sizeof(struct cli_command_st));
    if (ret != CLI_OK) {
        printf("timedeal cli register fail\n");
        return ret;
    }

    return CLI_OK;
}